package com.techq.lib.security;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class MD5 {
	
	
	private final char[] digits = { '0', '1', '2', '3', '4', '5', '6',
			'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	
	
	/**
	 * ZH: 获得随机MD5
	 * EN: get random md5
	 * @return
	 * StringBuilder
	 */
	public StringBuilder getRandomString() {
		Random r = new Random(System.nanoTime());

		StringBuilder hash = new StringBuilder();

		for (int i = 0; i < 32; i++) {
			int next = r.nextInt(35);
			char c;
			if (next < 25) {
				c = (char) ((int) 'A' + next);
				hash.append(c);
			} else {
				hash.append(next - 25);
			}
		}
		return hash;
	}

	
	
	

	/**
	 * EN: generate md5 with content
	 * ZH: 输入内容产生该内容的MD5
	 * @param content
	 * @return md5
	 * String
	 */
	public String generateDigest(String content) {
		MessageDigest digest = null;
		try {
			digest = java.security.MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace(System.out);
		}
		digest.update(content.getBytes());
		
		byte[] hash = digest.digest();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < hash.length; i++) {
			sb.append(getByteAsHexString(hash[i]));
		}
		return sb.toString();
	}
	
	
	public String generateDigest(String content, String algorithm) {
		MessageDigest digest = null;
		try {
			digest = java.security.MessageDigest.getInstance(algorithm);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace(System.out);
		}
		digest.update(content.getBytes());
		
		byte[] hash = digest.digest();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < hash.length; i++) {
			sb.append(getByteAsHexString(hash[i]));
		}
		return sb.toString();
	}
	
	
	
	
	/**
	 * EN： use the already MessageDigest update md5 
	 * ZH: 使用已有的摘要信息来更新内容的MD5 但内容很大的时候，就不需要从头开始计算
	 *     只需要把剩余的内容加入，再更新一下就行了
	 * @param digest
	 * @param addedContent
	 * @return
	 * String
	 */
	public String update(MessageDigest digest, String addedContent) {
		digest.update(addedContent.getBytes());
		byte[] hash = digest.digest();
		
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < hash.length; i++) {
			sb.append(getByteAsHexString(hash[i]));
		}
		return sb.toString();
	}

	private String getByteAsHexString(byte b) {
		char[] buf = new char[2];
		int radix = 1 << 4;
		int mask = radix - 1;
		buf[1] = digits[(int) (b & mask)];
		b >>>= 4;
		buf[0] = digits[(int) (b & mask)];
		return new String(buf);
	}
	
	public static void main(String[] args) {
		MD5 md5 = new MD5();
		String a = md5.generateDigest("chen1234", "sha1");
		System.out.println(a);
		String b = md5.generateDigest(a, "sha1");
		System.out.println(b);
		String c = md5.generateDigest(b + "1312562840" + "BUW3I5", "sha1");
		System.out.println(c);
		//9a0364b9e99bb480dd25e1f0284c8555
		//6858851eee0e05f318897984757b59dc
		//System.out.println(md5.generateDigest("contentcontent"));
		//System.out.println(md5.getRandomString());
	}
	
	
	
	
}
